<template>
	<view class="path-container" style="background-color: #fff;height: 100%;">
		<u-navbar :border-bottom="false" title="企业注册" :title-size="36" :title-bold="true" :title-color="textColor">
		</u-navbar>
		<view class="u-p-l-35 u-p-r-35">
			<Identify-Auth :registerType="1" ref="authShop"></Identify-Auth>
		</view>
		<set-submit-bt text="立即注册"  @click="registerShopFn"/>
		<akLoading isFullScreen :active="loading"></akLoading>
	</view>

</template>

<script>
	import IdentifyAuth from '@/components/Identify-Auth/index.vue';
	import SetSubmitBt from '@/components/set-submit-bt/index.vue'
	import { useShopStore } from '@/store/shop.js';
	import { mapState } from 'pinia';
	export default{
		components:{IdentifyAuth,SetSubmitBt},
		data(){
			return {
				loading:false,
			}
		},
		computed:{
			...mapState(useShopStore, {shoplogin: 'login'})
		},
		methods:{
			async registerShopFn(){
				this.loading = true;
				if(this.$refs.authShop.tabbarIndex === 0&&this.$refs.authShop.bussnessImageId== ''){
					this.loading = false;
					this.$u.toast('请先上传营业执照');
					return false;;
				};
				if(this.$refs.authShop.tabbarIndex === 0&&!this.$refs.authShop.idBusinessIdentify){
					this.loading = false;
					this.$u.toast('未能识别出营业执照信息，请重新上传识别');
					return false;;
				};
				if(this.$refs.authShop.tabbarIndex === 0&&this.$refs.authShop.businessForm.regionId === 0){
					this.loading = false;
					this.$u.toast('请选择营业区域');
					return false;;
				};
				const validResult = await this.$refs.authShop.validateShop();
				if(!validResult){
					this.loading = false;
					return false;
				};
				
				const res = await this.$newHttp.post('api/Shop/Signup', this.$refs.authShop.businessForm);
				this.loading = false;
				if (res.code == 200) {
					// 获取该用户得店铺
					const result = await this.$newHttp.get('api/User/GetShopsByUser' );
					if(result.code === 200){
						if (result.data.length!==0) {
							await this.shoplogin(result.data);
						}
					}
					this.$u.toast('注册成功', 1000, 'success');
					this.$mHelper.delayedJump('/pages/mine/index', 'reLaunch');
				}else if(res.code === 30904){
					this.$u.toast('企业自动校验失败，已转为人工审核，请耐心等候审核结果~');
					this.$mHelper.delayedJump('/pages/mine/index', 'reLaunch');
				}else{
					this.$u.toast('注册失败，错误信息'+res.erroMessage, 1000)
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background: #fff;
	}
</style>